random_set_state

This function sets the state of the internal random number generator to the previously saved state contained in the given string.

bool random_set_state(string state)

Parameters:
state
A string containing a state previously retrieved with the random_get_state function, not necessarily during the same execution round.

Return value:
true on success, false on failure.

Remarks:
A computer can never be truly random. This is why the name of the family of algorithms that deal with randomness is pseudo random number generators. Pseudo random number generators work by taking an initial random seed from the user, which is supposed to be a unique data chunk. This is used by the generator to produce a subsequent series of numbers that seem to be random. However, if the same seed is passed to the generator it'll produce the same series of numbers each time. By saving and loading the current state of the random number generator, we can recreate the same series of random numbers starting from any given point in time. This is useful if you have a simulation based on randomness, for instance, and you wish to be able to recreate the exact same series of events.

When BGT starts it automatically initializes the random number generator with a unique seed, so the initial state of the generator will be different each time the engine is launched. Therefore, if you wish to reproduce the same series of numbers you must remember the state from which you want to continue and explicitly set the generator to this state.

Example:
void main()
{

// Begin by retrieving the state from the random number generator.
string initial_state=random_get_state();

// Generate three random numbers between 1 and 10.
alert("Numbers", random(1, 10) + ", " + random(1, 10) + ", " + random(1, 10) + ".");

// Restore the random number generator to the old state that we've saved.
random_set_state(initial_state);

// Generate three new random numbers between 1 and 10. Notice that they are the exact same as before.
alert("Numbers", random(1, 10) + ", " + random(1, 10) + ", " + random(1, 10) + ".");
}